package com.project.isaacwiki.service.impl;

import com.project.isaacwiki.dto.UserLogin;
import com.project.isaacwiki.dto.UserRegister;
import com.project.isaacwiki.mapper.MapperLogin;
import com.project.isaacwiki.pojo.ImgPath;
import com.project.isaacwiki.pojo.Users;
import com.project.isaacwiki.service.ServiceLogin;
import com.project.isaacwiki.util.AliyunOSSOperator;
import com.project.isaacwiki.util.JwtUtil;
import com.project.isaacwiki.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Service
public class ServiceLoginImpl implements ServiceLogin {

    @Autowired
    private MapperLogin mapperLogin;

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @Override
    public Result login(String username, String password) {
        Users users = mapperLogin.login(username, password);
        if (users != null) {
            if (users.getState() == 1) {
                Map<String, Object> claims = new HashMap<>();
                claims.put("id", users.getId());
                claims.put("username", users.getUsername());
                String jwt = JwtUtil.generateToken(claims);
                ImgPath imgPath = mapperLogin.queryImage(users.getId());
                UserLogin userLogin = new UserLogin(users.getId(), users.getUsername(), users.getName(), imgPath.getPath(), jwt);
                return Result.Success((userLogin));
            } else {
                return Result.Error("用户停用中");
            }
        }
        return Result.Error("用户名或密码错误");
    }

    @Override
    public Result uploadImage(MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            try {
                return Result.Success(aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename(), "image"));
            } catch (Exception e) {
                return Result.Error("上传失败");
            }
        }
        return Result.Error("图片上传失败");
    }

    @Override
    @Transactional
    public Result register(UserRegister userRegister) throws Exception {
        String imgUrl = userRegister.getImgUrl();
        Users user = userRegister.getUser();
        if (user != null && imgUrl != null) {
            user.setState(1);
            String fileName = imgUrl.substring(imgUrl.lastIndexOf("/") + 1);
            if (mapperLogin.queryUser(user.getUsername()) != null) {
                aliyunOSSOperator.delete(fileName, "image");
                return Result.Error("用户已存在");
            }
            if (mapperLogin.register(user) == 1) {
                if (mapperLogin.registerImgPath(user.getId(), imgUrl) == 1) {
                    return Result.Success("注册成功");
                }
                aliyunOSSOperator.delete(fileName, "image");
                throw new Exception("注册失败");
            }
            aliyunOSSOperator.delete(fileName, "image");
            throw new Exception("注册失败");
        }
        if (imgUrl != null) {
            String fileName = imgUrl.substring(imgUrl.lastIndexOf("/") + 1);
            aliyunOSSOperator.delete(fileName, "image");
        }
        throw new Exception("信息不全，注册失败");
    }
}
